﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Net;


namespace Exhibition.Common.Helper
{
    public class EmailHelper
    {

        #region 邮件属性
        private string userName;   //电子邮箱用户名

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string password;        //电子邮箱密码

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        private string from;            //发件者邮箱

        public string From
        {
            get { return from; }
            set { from = value; }
        }
        private string smtpServerName;
        /// <summary>
        /// SMTP服务器名称
        /// </summary>
        public string SMTPServerName
        {
            set { smtpServerName = value; }
            get { return smtpServerName; }
        }
        public bool IsBodyHtml
        {
            get;
            set;
        }


        public string mailTo { get; set; }
        public string subject { get; set; }
        public string body { get; set; }

        #endregion
        /// <summary>
        /// 初始化的时候就可以把所有属性加载进去EmailHelper
        /// </summary>
        /// <param name="userName">发件者电子邮件用户名</param>
        /// <param name="password">发件者电子邮件密码</param>
        /// <param name="from">发件人电子邮箱</param>
        /// <param name="mailTo">收件人电子邮箱</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="smtpServerName">例如smtp.163.com</param>
        public EmailHelper(string userName, string password, string from, string mailTo, string subject, string body, string smtpServerName,bool isBodyHtml)
        {
            this.userName = userName;
            this.password = password;
            this.from = from;
            this.mailTo = mailTo;
            this.subject = subject;
            this.body = body;
            this.smtpServerName = smtpServerName;
            this.IsBodyHtml = isBodyHtml;
        }


        /// <summary>
        /// 发送电子邮件
        /// </summary>
        public bool Send()
        {

            try
            {
                MailAddress from = new MailAddress(From);
                MailAddress to = new MailAddress(mailTo);
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
                message.Subject = subject;
                message.IsBodyHtml = IsBodyHtml;
                message.Body = body;
                
                //if (file.Length != 0)//发送附件（多个附件的文件名称我用 | 隔开的，所以此处这样写）  
                //{



                //    for (int i = 0; i < file.Length; i++)
                //    {

                //        if (file[i].Length > 0)
                //        {

                //            string filename = file[i].FullName;
                //            try
                //            {
                //                message.Attachments.Add(new Attachment(filename));
                //            }
                //            catch (Exception e) { }
                          

                //        }

                //    }

                //}  

                SmtpClient client = new SmtpClient(smtpServerName);
                client.Credentials = new NetworkCredential(userName, password);
                client.Send(message);
                return true;
            }
            catch (System.Net.Mail.SmtpException e)
            {
                LogHelper.Error("邮件发送失败：\n 用户名：" + userName + "\n 密码：" + password + "\n 邮件内容：" + body + "\n 错误信息:" + e.StackTrace);
                return false;
            }


        }





    }

}
